﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XChip_Common
{
  public  class XChipAwbChart
    {

        /// <summary>
        /// 画板宽度
        /// </summary>
        public int BoardWidth { get; set; }
        /// <summary>
        /// 画板高度
        /// </summary>
        public int BoardHeight { get; set; }
        /// <summary>
        /// 画板背景颜色
        /// </summary>
        public Color BoardColor { get; set; }
        /// <summary>
        /// 画图区域颜色
        /// </summary>
        public Color AreasColor { get; set; }
        /// <summary>
        /// 曲线图颜色
        /// </summary>
        public Color GraphColor { get; set; }
        /// <summary>
        /// 坐标轴颜色
        /// </summary>
        public Color AxisColor { get; set; }
        /// <summary>
        /// 刻度线颜色
        /// </summary>
        public Color ScaleColor { get; set; }

        /// <summary>
        /// 当前绘制的图
        /// </summary>
        public Bitmap CurrentImage { get; set; }

        /// <summary>
        /// 垂直（纵向）边距（画图区域距离左右两边长度）
        /// </summary>
        public int VerticalMargin { get; set; }
        /// <summary>
        /// 平行（横向）边距（画图区域距离左右两边长度）
        /// </summary>
        public int HorizontalMargin { get; set; }
        /// <summary>
        /// X轴刻度线数量
        /// </summary>
        public int XScaleCount { get; set; }
        /// <summary>
        /// Y轴刻度线数量
        /// </summary>
        public int YScaleCount { get; set; }
        /// <summary>
        /// 画园的坐标x
        /// </summary>
        public long sum_x;
        /// <summary>
        /// 画园的坐标y
        /// </summary>
        public long sum_y;

        public XChipAwbChart(int width, int height, Color boradColor) {
            this.BoardWidth = width;
            this.BoardHeight = height;
            this.BoardColor = boradColor;

            //默认值
            this.XScaleCount = 8;
            this.YScaleCount = 8;
        }


        public Image GetCurrentGraph(List<XChipPoint> data, float xRange, float yRange, bool isFill) {
            CurrentImage = new Bitmap(BoardWidth, BoardHeight);
            Graphics g = Graphics.FromImage(CurrentImage);
            g.SmoothingMode = SmoothingMode.AntiAlias;   //反锯齿
            g.Clear(BoardColor);

            //1.确定曲线图区域
            int iAreaWidth = BoardWidth - 2 * HorizontalMargin;              //画图区域宽度
            int iAreaHeight = BoardHeight - 2 * VerticalMargin;              //画图区域高度
            double x_Offset = iAreaWidth / XScaleCount /(double)32 ;                 //实际值与坐标值的倍数
            double y_Offset = iAreaHeight / YScaleCount /(double)32;                 //坐标一格为int（iAreaWidth / XScaleCount）实际一格为(double)(256/8)
            Point pAreaStart = new Point(HorizontalMargin, VerticalMargin);  //画图区域起点
            Point pAreaEnd = new Point(BoardWidth - HorizontalMargin, BoardHeight - VerticalMargin);             //画图区域终点
            Point pOrigin = new Point(HorizontalMargin, BoardHeight - VerticalMargin);  //原点
            Rectangle rectArea = new Rectangle(pAreaStart, new Size(iAreaWidth, iAreaHeight));
            SolidBrush sbAreaBG = new SolidBrush(AreasColor);
            g.FillRectangle(sbAreaBG, rectArea);

            sbAreaBG.Dispose();

            //2.确定坐标轴
            Pen penAxis = new Pen(AxisColor, 1);
            penAxis.EndCap = LineCap.ArrowAnchor;
            g.DrawLine(penAxis, pOrigin, new Point(pAreaStart.X, pAreaStart.Y - VerticalMargin / 2));
            g.DrawLine(penAxis, pOrigin, new Point(pAreaEnd.X + HorizontalMargin / 2, pAreaEnd.Y));

            penAxis.Dispose();

            //3.确定刻度线和标签
            Pen penScale = new Pen(ScaleColor, 1);
            penScale.DashStyle = DashStyle.Dot;
            int fontSize = 8;
            for (int i = 0; i <= XScaleCount; i++)
            {
                int x = i * (iAreaWidth / XScaleCount) + pAreaStart.X;
                g.DrawLine(penScale, x, pAreaStart.Y, x, pAreaEnd.Y);
                string lbl =( i ==8? (i * (int)(xRange / XScaleCount)-1).ToString("x2"): (i * (int)(xRange / XScaleCount)).ToString("x2"));                
                if (i != 0)
                { g.DrawString(lbl, new Font("微软雅黑", fontSize, FontStyle.Regular), new SolidBrush(AxisColor), new Point(x - fontSize, pAreaEnd.Y + VerticalMargin / 9)); }
            }
            for (int i = 0; i <= YScaleCount; i++)
            {
                int y = pAreaEnd.Y - (i * (iAreaHeight / YScaleCount));
                g.DrawLine(penScale, pAreaStart.X, y, pAreaEnd.X, y);
                string lbl =(i==8? (i * (int)(yRange / YScaleCount)-1).ToString("x2"): (i * (int)(yRange / YScaleCount)).ToString("x2"));
                g.DrawString(lbl, new Font("微软雅黑", fontSize, FontStyle.Regular), new SolidBrush(AxisColor), new Point(pAreaStart.X - (fontSize * lbl.Length) - HorizontalMargin / 9, y - fontSize / 2));
            }

            Pen penGraph = new Pen(GraphColor, 2);
            for (int i = 0; i < data.Count; i++)
            {
                if (data[i].isColor)
                {
                    int data_x = (int)Math.Ceiling((data[i].Img_StartX) * x_Offset) + HorizontalMargin;
                    int data_y = (int)Math.Round(BoardHeight - (data[i].Img_EndY * y_Offset) - VerticalMargin);//画图是自上向下的，所以y这里从end开始画
                    int data_w = (int)Math.Round((data[i].Img_EndX - data[i].Img_StartX) * x_Offset);
                    int data_h = (int)Math.Round((data[i].Img_EndY - data[i].Img_StartY) * y_Offset);

                    g.DrawRectangle(penGraph, new Rectangle(data_x, data_y, data_w, data_h));
                    if (isFill)
                    {
                        g.DrawString(data[i].Img_Num, new Font("微软雅黑", fontSize, FontStyle.Regular), new SolidBrush(AxisColor), new Point((int)(data_x), (int)(data_y)));

                    }
                }
                else {
                    int data_x = (int)Math.Ceiling((data[i].Img_StartX) * x_Offset) + HorizontalMargin;
                    int data_y = (int)Math.Round(BoardHeight - (data[i].Img_EndY * y_Offset) - VerticalMargin);//画图是自上向下的，所以y这里从end开始画
                    int data_w = (int)Math.Round((data[i].Img_EndX - data[i].Img_StartX) * x_Offset);
                    int data_h = (int)Math.Round((data[i].Img_EndY - data[i].Img_StartY) * y_Offset);
                    var penGraph1 = new Pen(Color.Blue, 2);
                    g.DrawRectangle(penGraph1, new Rectangle(data_x, data_y, data_w, data_h));
                }     
               
            }
            if (sum_x>0&&sum_y>0)
            {
                Brush brush = new SolidBrush(Color.Red);
                int yuan_x = (int)Math.Round(sum_x*x_Offset + HorizontalMargin)-13;
                int yuan_y = (int)Math.Round(BoardHeight - sum_y* y_Offset - VerticalMargin)-13;
                g.FillEllipse(brush, yuan_x, yuan_y, 26, 26);
                g.SmoothingMode = SmoothingMode.AntiAlias;
            }
           

            return CurrentImage;
        }


    }


    public class XChipPoint {
        public int Img_StartX;
        public int Img_StartY;
        public int Img_EndX;
        public int Img_EndY;
        public string Img_Num;
        //变颜色
        public bool isColor= true;
    }

}
